package com.cc.cbdb.activity;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.cc.cbdb.R;
import com.cc.cbdb.common.constants.Constant;
import com.cc.cbdb.common.utils.StringUtil;

public class RelationQueryActivity extends AppCompatActivity implements View.OnClickListener{

    private LinearLayout ll_btn,ll_input,ll_option;
    private TextView titleBar,tv_relative;
    private EditText et_personName,et_relative;
    private Button btn_back,btn_buildGraph;;

    private Activity mContext;
    private int mMode;
    private String mCheckedIds,mPersonId,mCheckedId,mPersonName;
    private final int VISIBLE = View.VISIBLE;
    private final int INVISIBLE = View.INVISIBLE;
    private final int GONE = View.GONE;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_relation_query);
        mContext = this;
        initUI();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        String param1 = null;
        String param2 = null;
        String param3 = null;
        if (data != null){
            param1 = data.getStringExtra("checkedIds");
            param2 = data.getStringExtra("param2");
            param3 = data.getStringExtra("personId");
            mCheckedId = data.getStringExtra("param1");

        }
        switch (resultCode){
            case 7:
                if (StringUtil.isNotNull(param1)) {
                    mCheckedIds = param1;
                    et_relative.setText("已选择"+param1.split(",").length+"项");
                }else {
                    et_relative.setText("");
                }
                break;
            case 8:
                et_relative.setText(param2);
                break;
            case 9:
                mPersonId = param3;
                mPersonName = param2;
                et_personName.setText(param2);
                break;
            case 10:
                if (StringUtil.isNotNull(param1)) {
                    mPersonId = param1;
                    et_personName.setText("选择了"+param1.split(",").length+"个人");
                }else {
                    et_personName.setText("");
                }
                break;
        }
    }

    private void initUI() {

        ll_btn = findViewById(R.id.ll_btn);
        ll_input = findViewById(R.id.ll_input);
        ll_option = findViewById(R.id.ll_option);

        titleBar = findViewById(R.id.titleBar);
        tv_relative = findViewById(R.id.tv_relative);

        et_personName = findViewById(R.id.et_personName);
        et_relative = findViewById(R.id.et_relative);

        Button btn_query = findViewById(R.id.btn_query);
        Button btn_relative = findViewById(R.id.btn_relative);
        Button btn_social = findViewById(R.id.btn_social);
        btn_buildGraph = findViewById(R.id.btn_buildGraph);
        Button btn_relationShip = findViewById(R.id.btn_relationShip);
        btn_back = findViewById(R.id.btn_back);

        et_personName.setOnClickListener(this);
        btn_query.setOnClickListener(this);
        btn_relative.setOnClickListener(this);
        btn_social.setOnClickListener(this);
        btn_buildGraph.setOnClickListener(this);
        btn_relationShip.setOnClickListener(this);
        btn_back.setOnClickListener(this);
        et_relative.setOnClickListener(this);
    }


    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.et_personName:
                Intent intent1;
                if (mMode == 3) {
                    intent1 = new Intent(mContext,SelectedActivity.class);
                    intent1.putExtra("title",Constant.TITLE.TITLE_SELECTPERSON);
                    intent1.putExtra("tabName",Constant.SVTAB.SV_NAMES_TAB);
                }else {
                    intent1 = new Intent(mContext, SearchActivity.class);
                    intent1.putExtra("title",Constant.TITLE.TITLE_SELECTPERSON);
                    intent1.putExtra("tabName",Constant.SVTAB.SV_NAMES_TAB);
                    intent1.putExtra("isSelect",true);
                }
                startActivityForResult(intent1,9);
                break;
            case R.id.btn_relative:
                mMode = 1;
                chooseModeShow();
                break;
            case R.id.btn_social:
                mMode = 2;
                chooseModeShow();
                break;
            case R.id.btn_relationShip:
                mMode = 3;
                chooseModeShow();
                break;
            case R.id.btn_query:
                Intent intent2 = new Intent(mContext, SearchActivity.class);
                intent2.putExtra("isShowSeachView",false);
                intent2.putExtra("isLoading",true);
                intent2.putExtra("param1",mPersonId);
                if (mMode == 1) {
                    intent2.putExtra("title",Constant.TITLE.TITLE_KINSHIPRESULT);
                    intent2.putExtra("param2",mCheckedIds);
                }else if (mMode == 2){
                    intent2.putExtra("title",Constant.TITLE.TITLE_ASSOCRESULT);
                    intent2.putExtra("param2",mCheckedId);
                }
                startActivity(intent2);
                break;
            case R.id.btn_back:
                btn_back.setVisibility(INVISIBLE);
                ll_btn.setVisibility(VISIBLE);
                ll_input.setVisibility(INVISIBLE);
                ll_option.setVisibility(INVISIBLE);
                btn_buildGraph.setVisibility(GONE);
                titleBar.setText("人物关系查询");
                break;
            case R.id.et_relative:
                Intent intent;
                if (mMode == 1) {
                    intent = new Intent(mContext,SelectedActivity.class);
                    intent.putExtra("title",Constant.TITLE.TITLE_KINSHIP);
                    intent.putExtra("tabName",Constant.SVTAB.SV_KINSHIP_TAB);
                    startActivityForResult(intent,7);
                } else if(mMode == 2) {
                    intent = new Intent(mContext, X5WebViewActivity.class);
                    intent.putExtra("title", Constant.TITLE.TITLE_QASSOC);
                    intent.putExtra("url",Constant.DOMAIN_PROJECT+Constant.HTML.HTML_QASSOC);
                    startActivityForResult(intent,8);
                }
                break;
            case R.id.btn_buildGraph:
                Intent intent3 = new Intent(mContext, X5WebViewActivity.class);
                intent3.putExtra("title",Constant.TITLE.TITLE_RELATIONGRAPH);
                StringBuilder sb = new StringBuilder(Constant.DOMAIN_PROJECT);
                sb.append(Constant.HTML.HTML_RELATIONGRAPH);
                sb.append("#");
                sb.append(mPersonId);
                intent3.putExtra("url",sb.toString());
                startActivity(intent3);
                break;
        }
    }

    /**
     * 选择UI显示
     */
    private void chooseModeShow(){
        ll_btn.setVisibility(INVISIBLE);
        ll_input.setVisibility(VISIBLE);
        ll_option.setVisibility(VISIBLE);
        et_relative.setVisibility(VISIBLE);
        ll_option.setVisibility(VISIBLE);
        if (mMode == 1) {
            tv_relative.setText("亲属关系：");
            et_relative.setHint("点击选择亲属关系");
            titleBar.setText("亲属关系查询");
        }else if (mMode == 2){
            tv_relative.setText("社会关系：");
            et_relative.setHint("点击选择社会关系");
            titleBar.setText("社会关系查询");
        }else if (mMode == 3){
            btn_buildGraph.setVisibility(VISIBLE);
            tv_relative.setVisibility(GONE);
            et_relative.setVisibility(GONE);
            ll_option.setVisibility(GONE);
        }
        mCheckedIds = "";
        et_relative.setText("");
        btn_back.setVisibility(VISIBLE);
    }
}
